package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 套餐管理
 */
@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 添加套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> add(@RequestBody SetmealDto setmealDto){
        log.info("添加套餐。。。。setmealDto ：{}",setmealDto.toString());
        //调用service层
        setmealService.addSetmealAndDish(setmealDto);
        return R.success("添加套餐成功");
    }

    /**
     * 套餐管理分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(Integer page,Integer pageSize,String name){
        log.info("套餐分页查询.....page :{} , pageSize :{} ,name:{}",page,pageSize,name);
        //构建分页构造器
        Page<Setmeal> setmealPage = new Page<>(page,pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>();
        //将setmealPage中的数据写入setmealDtoPage,不包括records
        BeanUtils.copyProperties(setmealPage,setmealDtoPage,"records");

        //构造条件查询器
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        //构建查询条件
        queryWrapper.like(name!= null,Setmeal::getName,name);
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(setmealPage,queryWrapper);

        //获取setmealPage中的records
        List<Setmeal> records = setmealPage.getRecords();

        List<SetmealDto> list = records.stream().map((item)->{
            //创建一个SetmealDto对象
            SetmealDto setmealDto = new SetmealDto();
            //将item中的数据传给setmealDto
            BeanUtils.copyProperties(item,setmealDto);
            //获取records中的套餐分类id
            Long categoryId = item.getCategoryId();
            //根据categoryId查询数据库,获得category对象
            Category category = categoryService.getById(categoryId);
            //获取category对象中的分类名称
            String categoryName = category.getName();
            //将categoryName写入到
            setmealDto.setCategoryName(categoryName);
            //返回setmealDto
            return setmealDto;
        }).collect(Collectors.toList());

        setmealDtoPage.setRecords(list);

        return R.success(setmealDtoPage);
    }


    /**
     * 修改套餐信息时的数据回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> setmealInfo(@PathVariable Long id){
        log.info("修改套餐信息时的数据回显 id : {}",id);
        //调用service层
        SetmealDto setmealDto = setmealService.setmealDtoInfo(id);
        return R.success(setmealDto);
    }

    /**
     * 修改套餐
     * @param setmealDto
     * @return
     */
    @PutMapping
    public R<String> updateStemeal(@RequestBody SetmealDto setmealDto){
        log.info("修改套餐。。。setmealDto：{}",setmealDto.toString());
        //调用service层
        setmealService.updatesetmealWithDish(setmealDto);
        return R.success("修改套餐成功");

    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> deleteById(@RequestParam List<Long> ids){
        log.info("删除套餐.... ids = {}",ids.toString());
        //调用service层
        setmealService.deleteSetmeal(ids);
        return R.success("删除套餐成功");
    }

    /**
     * 批量修改套餐起售状态
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable Integer status,@RequestParam List<Long> ids){
        log.info("批量修改套餐起售状态.... status:{}  ids:{}",status,ids.toString());
        setmealService.updateStatus(status,ids);
        return R.success("修改状态成功");
    }




    /**
     * 在app端展示套餐信息
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    public R<List<SetmealDto>> list(Setmeal setmeal){
        log.info("套餐查询。。。。setmeal:{}",setmeal.toString());
        //构建条件查询器
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        queryWrapper.eq(Setmeal::getStatus,1);
        List<Setmeal> list = setmealService.list(queryWrapper);

        List<SetmealDto> setmealDtoList = list.stream().map((item)->{
            SetmealDto setmealDto = new SetmealDto();
            //将list中的数据赋值给setmealDto
            BeanUtils.copyProperties(item,setmealDto);
            //setmealDto中没有分类名称
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if (category!=null){
                //获取分类名称
                String categoryName = category.getName();
                //将分类名称赋值给setmealDto
                setmealDto.setCategoryName(categoryName);
            }
            //再将套餐对应的菜品信息写入到setmealDto中
            LambdaQueryWrapper<SetmealDish> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(item.getId()!=null,SetmealDish::getSetmealId,item.getId());
            queryWrapper1.orderByDesc(SetmealDish::getCreateTime);
            List<SetmealDish> setmealDishList = setmealDishService.list(queryWrapper1);
            setmealDto.setSetmealDishes(setmealDishList);
            return setmealDto;
        }).collect(Collectors.toList());
        return R.success(setmealDtoList);
    }
}
